part of my_base_lib;

class ProgressDialog extends Dialog {
  final bool cancelable;

  ProgressDialog({Key? key, this.cancelable = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _LoadingProgress(cancelable);
  }
}

class _LoadingProgress extends StatefulWidget {
  final bool cancelable;

  const _LoadingProgress(this.cancelable, {Key? key}) : super(key: key);

  @override
  _LoadingProgressState createState() => _LoadingProgressState();
}

class _LoadingProgressState extends State<_LoadingProgress> {
  double _angle = 0.0;
  bool _dispose = false;

  @override
  void initState() {
    super.initState();
    _dispose = false;
    _startRotation();
  }

  void _startRotation() {
    if (!_dispose) {
      Future.delayed(const Duration(milliseconds: 50), () {
        if (!_dispose) {
          setState(() {
            _angle += 0.1; // 调整旋转速度和方向
            if (_angle >= 360) {
              _angle = 0;
            }
          });
          _startRotation();
        }
      });
    }
  }

  @override
  void dispose() {
    super.dispose();
    _dispose = true;
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        type: MaterialType.transparency,
        child: Align(
            alignment: Alignment.center,
            child: WillPopScope(
              onWillPop: () async {
                // 阻止通过返回键关闭Dialog
                return widget.cancelable;
              },
              child: Container(
                padding: EdgeInsets.all(50),
                width: 150,
                height: 150,
                decoration: const BoxDecoration(
                  color: Color.fromARGB(255, 77, 77, 77),
                  borderRadius: BorderRadius.all(Radius.circular(15)),
                ),
                child: Transform.rotate(
                  angle: _angle,
                  child: Image.asset('assets/image/loading.png'),
                ),
              ),
            )));
  }
}
